<div class="layui-fluid">
    <div class="layui-card">
        <!-- 搜索表单区 -->
        <form class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="table_model-search-form">
            <div class="layui-form-item">


                <!-- 默认表单 -->
                <div class="layui-inline">
                    <label class="layui-form-label">表编号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="table_id" placeholder="请输入表编号" autocomplete="off" class="layui-input">
                    </div>
                </div>


                <!-- 默认表单 -->
                <div class="layui-inline">
                    <label class="layui-form-label">表名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="table_key" placeholder="请输入表名" autocomplete="off" class="layui-input">
                    </div>
                </div>


                <!-- 默认表单 -->
                <div class="layui-inline">
                    <label class="layui-form-label">显示名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="table_name" placeholder="请输入显示名称" autocomplete="off" class="layui-input">
                    </div>
                </div>


                <!-- 默认表单 -->
                <div class="layui-inline">
                    <label class="layui-form-label">模型包名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="model_package" placeholder="请输入模型包名" autocomplete="off" class="layui-input">
                    </div>
                </div>


                <!-- 时间表单 -->
                <div class="layui-inline">
                    <label class="layui-form-label">创建时间</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="create_datetime_search_datetime" placeholder="yyyy-MM-dd HH:mm:ss">
                    </div>
                </div>


                <div class="layui-inline">
                    <button class="layui-btn btn-table_model-search" lay-submit lay-filter="btn-table_model-form-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>

        <!-- 列表区 -->
        <div class="layui-card-body">

            <!-- 头部工具栏模板 -->
            <script type="text/html" id="table_model_header_toolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn" lay-event="add">添加</button>
                    <button class="layui-btn layui-btn-danger" lay-event="dels">删除</button>
                </div>
            </script>

            <!-- 列表表格 -->
            <table id="table_model-table" lay-filter="table_model-table" style="width: 100%"></table>


            <!-- 每行操作按钮模板 -->
            <script type="text/html" id="table_model_row_toolbar">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="code"><i class="layui-icon layui-icon-app"></i>模型生成</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<!-- JS脚本区 -->
<script>
    layui.use(['table', 'form', 'layer', 'laydate', 'jquery', 'admin', 'formSelects'], function () {
        var form = layui.form, layer = layui.layer, laydate = layui.laydate, table = layui.table, admin = layui.admin, formSelects = layui.formSelects;

        form.render(null, 'table_model-search-form');


        //渲染创建时间日期时间选择器
        laydate.render({elem: '#create_datetime_search_datetime', type: 'datetime'});


        // 表单搜索监听
        form.on('submit(btn-table_model-form-search)', function (data) {
            console.log('table_model-search-form', data)
            var field = data.field;
            //执行重载
            table.reload('table_model-table', {
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });

        // 渲染表格数据
        table.render({
            id: 'table_model-table',
            elem: '#table_model-table',
            toolbar: '#table_model_header_toolbar',
            url: '/generator/table/list',
            loading: true,
            page: true, //开启分页
            cols: [[
                {type: 'checkbox', fixed: 'left'},


                // {field: 'table_id', title: '表编号'/*, event: 'table_id', templet: "#table_id_templet"*/},


                {field: 'table_key', title: '表名'/*, event: 'table_key', templet: "#table_key_templet"*/},


                {field: 'table_name', title: '显示名称'/*, event: 'table_name', templet: "#table_name_templet"*/},


                {field: 'model_package', title: '模型包名'/*, event: 'model_package', templet: "#model_package_templet"*/},


                {field: 'create_datetime', title: '创建时间'/*, event: 'create_datetime', templet: "#create_datetime_templet"*/},


                {title: '操作', width: 400, fixed: 'right', templet: '#table_model_row_toolbar'}
            ]],
            done: function () {
            }
        });

        // 监听表格工具栏事件
        table.on("toolbar(table_model-table)", eventListener);

        // 监听表格行事件
        table.on("tool(table_model-table)", eventListener);

        // 事件监听
        function eventListener(obj) {
            let event = obj.event;
            let data = obj.data;
            switch (event) {
                case 'add':
                    layui.$.post('/generator/table/editPage', function (str) {
                        layer.open({
                            type: 1,
                            title: '新建表模型',
                            maxmin: true,
                            area: ['100%', '100%'],
                            content: str,
                        })
                    });
                    break;
                case 'edit':
                    layui.$.post('/generator/table/editPage', {table_id: data.table_id}, function (str) {
                        layer.open({
                            type: 1,
                            title: '编辑表模型',
                            maxmin: true,
                            area: ['100%', '100%'],
                            content: str,
                        })
                    });
                    break;
                case 'dels':
                    let checkStatus = table.checkStatus('table_model-table');
                    let selectRow = checkStatus.data;
                    deleteRow(selectRow);
                    break;
                case 'del':
                    let row = obj.data;
                    deleteRow([row]);
                    break;
                case 'code':
                    layui.$.post('/generator/table/codePage', {table_id: data.table_id, port: 8080}, function (str) {
                        layer.open({
                            type: 1,
                            title: '模型代码生成',
                            maxmin: true,
                            area: ['100%', '100%'],
                            content: str,
                            cancel: function (index, layero) {
                                let logSocket = window.logSocket;
                                if (logSocket) {
                                    logSocket.close();
                                }
                                layer.close(index);
                            }
                        })
                    });
                    break;
                case 'download':
                    window.open('/generator/table/download?table_id=' + data.table_id);
                    break;
            }
            console.log('toolbar_event', obj)
        }

        // 删除行方法
        function deleteRow(selectRows) {
            if (selectRows.length <= 0) {
                layer.msg('请先钩选要删除的记录行！');
                return;
            }
            layer.confirm('确定要删除这' + selectRows.length + '条记录吗？',
                {icon: 3, title: '提示', btn: ['确认删除', '取消']},
                function (index) {
                    console.log(index);
                    layui.$.ajax({
                        url: '/generator/table/delete',
                        data: {
                            ids: selectRows.map((row) => row.table_id)
                        },
                        traditional: true,
                        type: 'POST',
                        success: function (result) {
                            if (result.success) {
                                layer.msg('删除成功', {icon: 6});
                                table.reload('table_model-table');
                            } else {
                                layer.msg('删除失败！' + result.message, {icon: 5});
                            }
                        }
                    });
                });

        }

    })
</script>